package cn.jiedh.app.meiweidk.strategy;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.HtmlUtil;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.entity.StrategyInfoBean;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;

/**
 * @name：攻略详情
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/21
 */

public class StrategyActivity extends BaseActivity {
    @BindView(R.id.bar_tv_title)
    TextView title;
    @BindView(R.id.strategy_web)
    WebView web;
    @BindView(R.id.strategy_title)
    TextView strategyTitle;
    @BindView(R.id.strategy_time)
    TextView strategyTime;
    @BindView(R.id.empty_nn_pic)
    ImageView emptyNnPic;
    @BindView(R.id.empty_nn_btn)
    TextView emptyNnBtn;

    private int raidersId;


    @Override
    protected int getLayoutView() {
        return R.layout.activity_strategy;
    }

    @Override
    protected void initView(View view) {
        if (getIntent().getExtras() != null) {
            title.setText("社区详情");
            raidersId = getIntent().getExtras().getInt("raidersId");
        }

        WebSettings settings = web.getSettings();
        settings.setAppCacheEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setSupportZoom(true);
        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("raidersId", raidersId);
        request(this, UrlUtils.RAIDERSINFO, map, false, new ResponseSuccess<StrategyInfoBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<StrategyInfoBean> response) {
                if (0 == response.getCode()) {
                    strategyTitle.setText(response.getData().getTitle());
                    strategyTime.setText(response.getData().getStartTime());
                    web.loadDataWithBaseURL(null, response.getData().getText(), HtmlUtil.MIME_TYPE, HtmlUtil.ENCODING, null);
                } else if (1 == response.getCode()) {
                    findViewById(R.id.msg_detail_empty).setVisibility(View.VISIBLE);
                    findViewById(R.id.mscrollview).setVisibility(View.GONE);
                    emptyNnPic.setImageResource(R.mipmap.nodate);
                    emptyNnBtn.setText("这条攻略飞走了");
                    T.showShort(response.getMessage());
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (web != null) {
            web.removeAllViews();
            // in android 5.1(sdk:21) we should invoke this to avoid memory leak
            // see (https://coolpers.github.io/webview/memory/leak/2015/07/16/
            // android-5.1-webview-memory-leak.html)
            ((ViewGroup) web.getParent()).removeView(web);
            web.setTag(null);
            web.clearHistory();
            web.destroy();
            web = null;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
